# Helper script to park the carriage (called from T0 and T1 macros)
[gcode_macro PARK_extruder]
description: TODO: Add description
gcode:
    SAVE_GCODE_STATE NAME=park0
    G90
    G1 X{printer.configfile.settings.stepper_x.position_min+5} F12000
    RESTORE_GCODE_STATE NAME=park0

[gcode_macro PARK_extruder1]
description: TODO: Add description
gcode:
    SAVE_GCODE_STATE NAME=park1
    G90
    G1 X{printer.configfile.settings.dual_carriage.position_max-5} F12000
    RESTORE_GCODE_STATE NAME=park1

# Activate the primary extruder
[gcode_macro T0]
description: TODO: Add description
gcode:
    {% if printer.dual_carriage.active_carriage == "CARRIAGE_1" %}
        PARK_extruder1
    {% endif %}
    {% set fan_speed = printer["gcode_macro M106"].swap_speed %}
    {% if fan_speed != -1 %}
        SET_FAN_SPEED FAN=fan_extruder SPEED={fan_speed}
#    {% else %}
        # Update core Klipper's fan speed to the fan speed of the active toolhead
        # Only do this if you have a sacrificial [fan] section
#        M106.1 S{printer["fan_generic fan_extruder"].speed * 255}
    {% endif %}
    SET_DUAL_CARRIAGE CARRIAGE=0
    ACTIVATE_EXTRUDER EXTRUDER=extruder
    SWAP_CLEAN
    G1 F12000
    SET_GCODE_OFFSET X=0 Y=0

[gcode_macro T1]
description: TODO: Add description
gcode:
    {% if printer.dual_carriage.active_carriage != "CARRIAGE_1" %}
        PARK_extruder
    {% endif %}
    {% set fan_speed = printer["gcode_macro M106"].swap_speed %}
    {% if fan_speed != -1 %}
        SET_FAN_SPEED FAN=fan_extruder1 SPEED={fan_speed}
#    {% else %}
        # Update core Klipper's fan speed to the fan speed of the active toolhead
        # Only do this if you have a sacrificial [fan] section
#        M106.1 S{printer["fan_generic fan_extruder1"].speed * 255}
    {% endif %}
    SET_DUAL_CARRIAGE CARRIAGE=1
    ACTIVATE_EXTRUDER EXTRUDER=extruder1
    SWAP_CLEAN
    G1 F12000
    SET_GCODE_OFFSET X=0 Y=0

[gcode_macro SWAP_CLEAN]
gcode:
    G1 E20 F200
    G1 E-2 F1800
